package com.tuanzi.loan.workflow.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.tuanzi.loan.workflow.entity.FormAttribute;
import com.tuanzi.loan.workflow.entity.FormBinding;
import com.tuanzi.loan.workflow.repository.FormAttributeRepository;
import com.tuanzi.loan.workflow.service.FormAttributeService;
import com.tuanzi.loan.workflow.service.WorkflowManagerService;
import com.tuanzi.loan.workflow.vo.ActivityResponse;

/**
 * @author Ray
 * @date 2018/01/22
 */
@Service
public class FormAttributeServiceImpl implements FormAttributeService {

	@Autowired
	private FormAttributeRepository formAttributeRepository;
	@Autowired
	private WorkflowManagerService workflowManagerService;

	// @Resource
	// private WorkflowManagerService workflowManagerService;
	//
	// @Resource
	// private FormBindingService formBindingService;
	//
	// @Override
	// public BaseResponse<String> saveFormAttributeList(FormAttributeSaveRequest
	// request) {
	//
	// BaseResponse<String> response = new
	// BaseResponse<>(ErrorResponseEnum.INVOKE_SUCCESS);
	//
	// FormAttribute record = new FormAttribute();
	// ConversionUtil.objectToObject(request, record);
	//
	// BaseResponse<FormAttribute> saveResponse = this.save(record);
	// if (saveResponse.getResult().isSuccess()) {
	// response.setResult(new BaseResult<>(true, "保存表单属性成功"));
	// } else {
	// response.setResult(new BaseResult<>(false, "保存表单属性失败"));
	// }
	//
	// return response;
	// }
	//
	// @Override
	// public BaseResponse<String>
	// saveFormAttributeList(List<FormAttributeSaveRequest> requestList) {
	//
	// BaseResponse<String> response = new
	// BaseResponse<>(ErrorResponseEnum.INVOKE_SUCCESS);
	//
	// for (FormAttributeSaveRequest formAttributeSaveRequest : requestList) {
	// FormAttribute record = new FormAttribute();
	// ConversionUtil.objectToObject(formAttributeSaveRequest, record);
	//
	// BaseResponse<FormAttribute> saveResponse = this.save(record);
	// if (!saveResponse.getResult().isSuccess()) {
	// throw new ServiceException(ErrorResponseEnum.SYSTEM_ERROR.getCode(),
	// "批量保存表单属性失败");
	// }
	// }
	//
	// response.setResult(new BaseResult<>(true, "批量保存表单属性成功"));
	//
	// return response;
	// }
	//
	// @Override
	// public BaseResponse<String> deleteAndSave(FormAttributeDeleteAndSaveRequest
	// request) {
	//
	// if (StringUtils.isBlank(request.getProcDefId())) {
	// throw new
	// ServiceException(ErrorResponseEnum.REQUEST_PARAMETER_INVALID.getCode(),
	// "procDefId不能为空");
	// }
	//
	// BaseResponse<String> response = new
	// BaseResponse<>(ErrorResponseEnum.INVOKE_SUCCESS);
	//
	// FormAttributeDeleteRequest deleteRequest = new FormAttributeDeleteRequest();
	// deleteRequest.setProcDefId(request.getProcDefId());
	// BaseResponse<String> deleteResponse = this.deleteByProDefId(deleteRequest);
	//
	// if (deleteResponse.getResult().isSuccess()) {
	// for (FormAttributeSaveRequest formAttributeSaveRequest :
	// request.getFormAttributeSaveRequestList()) {
	// BaseResponse<String> saveResponse =
	// this.saveFormAttributeList(formAttributeSaveRequest);
	// if (!saveResponse.getResult().isSuccess()) {
	// throw new ServiceException(ErrorResponseEnum.SYSTEM_ERROR.getCode(),
	// "保存表单属性失败");
	// }
	// }
	// response.setResult(new BaseResult<>(true, "保存表单属性成功"));
	// } else {
	// response.setResult(new BaseResult<>(false, "保存表单属性失败"));
	// }
	//
	// return response;
	// }
	//
	@Override
	public void deleteByProcDefId(String procDefId) {
		formAttributeRepository.deleteInBatch(formAttributeRepository.findAllByProcDefId(procDefId));
	}

	@Override
	public void deleteByFormBindingId(Long formBindingId) {
		formAttributeRepository.delete(formAttributeRepository.findAllByFormBindId(formBindingId));
	}
	//
	// @Override
	// public BaseResponse<FormAttributeListResponse>
	// listFormAttributeByField(FormAttributeQueryRequest request) {
	// BaseResponse<FormAttributeListResponse> response = new
	// BaseResponse<>(ErrorResponseEnum.INVOKE_SUCCESS);
	// FormAttributeListResponse listResponse = new FormAttributeListResponse();
	//
	// Example example = new Example(FormAttribute.class);
	// if (StringUtils.isNotEmpty(request.getProcDefId())) {
	// example.createCriteria().andEqualTo("procDefId", request.getProcDefId());
	// }
	// if (null != request.getFormBindId()) {
	// example.createCriteria().andEqualTo("formBindId", request.getFormBindId());
	// }
	// example.orderBy("sort");
	//
	// List<FormAttribute> formAttributeList = mapper.selectByExample(example);
	// List<FormAttributeResponse> formAttributeResponseList = new
	// ArrayList<>(formAttributeList.size());
	//
	// if (!CollectionUtils.isEmpty(formAttributeList)) {
	// FormAttributeResponse formAttributeResponse = null;
	// for (FormAttribute formAttribute : formAttributeList) {
	// formAttributeResponse = new FormAttributeResponse();
	// ConversionUtil.objectToObject(formAttribute, formAttributeResponse);
	// formAttributeResponseList.add(formAttributeResponse);
	// }
	// }
	//
	// listResponse.setFormAttributeResponseList(formAttributeResponseList);
	//
	// response.setResult(new BaseResult<>(listResponse));
	// return response;
	// }
	//
	// @Override
	// public BaseResponse<FormAttributeListResponse>
	// listFormAttribute(FormAttributeQueryRequest request) {
	//
	// BaseResponse<FormAttributeListResponse> response = new
	// BaseResponse<>(ErrorResponseEnum.INVOKE_SUCCESS);
	// FormAttributeListResponse listResponse = new FormAttributeListResponse();
	//
	// FormAttribute record = new FormAttribute();
	// record.setProcDefId(request.getProcDefId());
	// record.setActId(request.getActivityId());
	//
	// List<FormAttribute> formAttributeList = mapper.select(record);
	// List<FormAttributeResponse> formAttributeResponseList = new
	// ArrayList<>(formAttributeList.size());
	//
	// if (!CollectionUtils.isEmpty(formAttributeList)) {
	// FormAttributeResponse formAttributeResponse = null;
	// for (FormAttribute formAttribute : formAttributeList) {
	// formAttributeResponse = new FormAttributeResponse();
	// ConversionUtil.objectToObject(formAttribute, formAttributeResponse);
	// formAttributeResponseList.add(formAttributeResponse);
	// }
	// }
	//
	// // 获取该流程定义下的所有活动结点
	// AllActivityGetRequest allActivityGetRequest = new AllActivityGetRequest();
	// allActivityGetRequest.setProcDefId(request.getProcDefId());
	// BaseResponse<List<ActivityVO>> activityVOListResponse =
	// workflowManagerService.getAllActivity(allActivityGetRequest);
	// List<ActivityResponse> activityResponseList = new
	// ArrayList<>(activityVOListResponse.getResult().getData().size());
	// for (ActivityVO activityVO : activityVOListResponse.getResult().getData()) {
	// ActivityResponse activityResponse = new ActivityResponse();
	// activityResponse.setActivityId(activityVO.getActivityId());
	// activityResponse.setActivityName(activityVO.getActivityName());
	// activityResponseList.add(activityResponse);
	// }
	//
	// ActivityResponse activityResponse = new ActivityResponse();
	// activityResponse.setActivityId("endevent");
	// activityResponse.setActivityName("结束节点");
	// activityResponseList.add(activityResponse);
	//
	// FormBindingQueryRequest formBindingQueryRequest = new
	// FormBindingQueryRequest();
	// formBindingQueryRequest.setProcDefId(request.getProcDefId());
	// // 查询表单名称
	// DatatablesPageResponse<FormBindingQueryResponse> formBindingListResponse =
	// formBindingService.listFormBanding(formBindingQueryRequest);
	//
	// listResponse.setFormAttributeResponseList(formAttributeResponseList);
	// listResponse.setActivityResponseList(activityResponseList);
	// listResponse.setFormBindingQueryResponseList(formBindingListResponse.getData());
	//
	// response.setResult(new BaseResult<>(listResponse));
	//
	// return response;
	// }
	//
	// @Override
	// public BaseResponse<String>
	// saveByFormBinding(FormAttributeSaveByFormBindingReqeust request) {
	//
	// BaseResponse<String> response = new
	// BaseResponse<>(ErrorResponseEnum.INVOKE_SUCCESS);
	//
	// FormAttribute record = new FormAttribute();
	// record.setFormBindId(request.getFormBindId());
	//
	// List<FormAttribute> formAttributeList = this.findByParams(record);
	//
	// if (!CollectionUtils.isEmpty(formAttributeList)) {
	// for (FormAttribute formAttribute : formAttributeList) {
	// formAttribute.setFormBindId(request.getFormBindId());
	// formAttribute.setSort(request.getSort());
	// this.updateSelective(formAttribute);
	// }
	// response.setResult(new BaseResult<>(true, "更新成功"));
	// } else {
	//
	// AllActivityGetRequest allActivityGetRequest = new AllActivityGetRequest();
	// allActivityGetRequest.setProcDefId(request.getProcDefId());
	// BaseResponse<List<ActivityVO>> activityVOListResponse =
	// workflowManagerService.getAllActivity(allActivityGetRequest);
	//
	// for (ActivityVO activityVO : activityVOListResponse.getResult().getData()) {
	// FormAttributeSaveRequest saveRequest = new FormAttributeSaveRequest();
	// saveRequest.setActId(activityVO.getActivityId());
	// saveRequest.setFormBindId(request.getFormBindId());
	// saveRequest.setProcDefId(request.getProcDefId());
	// saveRequest.setRightCode(request.getFormBindId().toString());
	// saveRequest.setRightValue(FormAttribute.RIGHT_VALUE_READONLY);
	// saveRequest.setSort(request.getSort());
	// BaseResponse<String> saveResponse = this.saveFormAttributeList(saveRequest);
	// if (!saveResponse.getResult().isSuccess()) {
	// throw new ServiceException(ErrorResponseEnum.SYSTEM_ERROR.getCode(),
	// "同步表单属性失败");
	// }
	// }
	//
	// response.setResult(new BaseResult<>(true, "同步表单属性成功"));
	// }
	//
	// return response;
	// }
	//
	// @Override
	// public BaseResponse<String>
	// batchSaveFormAttribute(FormAttributeBatchSaveRequest request) {
	//
	// if (StringUtils.isEmpty(request.getFormAttributeStr())) {
	// return new BaseResponse<>(new BaseResult<>(false, "保存失败， 属性不能为空"));
	// }
	//
	// FormAttributeDeleteAndSaveRequest deleteAndSaveRequest = new
	// FormAttributeDeleteAndSaveRequest();
	// deleteAndSaveRequest.setProcDefId(request.getProcDefId());
	//
	// List<FormAttributeSaveRequest> formAttributeSaveRequestList = new
	// ArrayList<>();
	//
	// String[] formAttributeArray =
	// StringUtils.split(request.getFormAttributeStr(), "&");
	// for (int i = 0; i < formAttributeArray.length; i++) {
	// String formAttribute = formAttributeArray[i];
	// JSONObject jsonObject = JSONObject.fromObject(formAttribute);
	// FormAttributeSaveRequest formAttributeSaveRequest = new
	// FormAttributeSaveRequest();
	// formAttributeSaveRequest.setProcDefId(request.getProcDefId());
	// formAttributeSaveRequest.setActId(jsonObject.getString("actId"));
	// formAttributeSaveRequest.setRightCode(jsonObject.getString("rightCode"));
	// formAttributeSaveRequest.setRightValue(jsonObject.getInt("rightValue"));
	// formAttributeSaveRequest.setFormBindId(jsonObject.getLong("formId"));
	// formAttributeSaveRequest.setSort(i + 1);
	// formAttributeSaveRequestList.add(formAttributeSaveRequest);
	// }
	//
	// deleteAndSaveRequest.setFormAttributeSaveRequestList(formAttributeSaveRequestList);
	//
	// return this.deleteAndSave(deleteAndSaveRequest);
	// }
	//

	@Override
	public void saveByFormBinding(FormBinding formBinding) {
		List<FormAttribute> formAttributes = formAttributeRepository.findAllByFormBindId(formBinding.getId());
		if (CollectionUtils.isEmpty(formAttributes)) {

			List<ActivityResponse> activityResponses = workflowManagerService.findAllUserTaskActivity(formBinding.getProcDefId());
			for (ActivityResponse activityResponse : activityResponses) {
				FormAttribute formAttribute = new FormAttribute();
				formAttribute.setSort(formAttribute.getSort());
				formAttribute.setFormBindId(formBinding.getId());
				formAttribute.setActivityId(activityResponse.getActivityId());
				formAttribute.setProcDefId(formBinding.getProcDefId());
				formAttribute.setRightValue(FormAttribute.RIGHT_VALUE_READONLY);
				formAttributeRepository.save(formAttribute);
			}

		} else {

			for (FormAttribute formAttribute : formAttributes) {
				formAttributeRepository.save(formAttribute);
			}
		}
	}

	@Override
	public List<FormAttribute> save(List<FormAttribute> formAttributes) {
		return formAttributeRepository.save(formAttributes);
	}

	@Override
	public List<FormAttribute> findAllByProcDefId(String procDefId) {
		return formAttributeRepository.findAllByProcDefId(procDefId);
	}

	@Override
	public List<FormAttribute> findAllByProcDefIdAndFormBindId(String procDefId, Long formBindId) {
		return formAttributeRepository.findAllByProcDefIdAndFormBindId(procDefId, formBindId);
	}

	@Override
	public FormAttribute findOne(String procDefId, String activityId, Long formBindingId) {
		return formAttributeRepository.findByProcDefIdAndActivityIdAndFormBindId(procDefId, activityId, formBindingId);
	}

	@Override
	public FormAttribute save(FormAttribute formAttribute) {
		return formAttributeRepository.save(formAttribute);
	}
}
